package com.atguigu.tingshu.album.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.atguigu.tingshu.album.mapper.*;
import com.atguigu.tingshu.album.service.BaseCategoryService;
import com.atguigu.tingshu.common.cache.TsCache;
import com.atguigu.tingshu.model.album.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@SuppressWarnings({"all"})
public class BaseCategoryServiceImpl extends ServiceImpl<BaseCategory1Mapper, BaseCategory1> implements BaseCategoryService {

	@Autowired
	private BaseCategory1Mapper baseCategory1Mapper;

	@Autowired
	private BaseCategory2Mapper baseCategory2Mapper;

	@Autowired
	private BaseCategory3Mapper baseCategory3Mapper;


	@Autowired
	private BaseCategoryViewMapper baseCategoryViewMapper;

	@Autowired
	private BaseAttributeMapper baseAttributeMapper;

	@Override
	public List<BaseCategory3> findTopBaseCategory3(Long category1Id) {
		/*
			select * from base_category2 where category1_id = 1;
			select * from base_category3 where category2_id in (101,102,103) and is_top = 1 limit 7;
		 */
		List<Long> category2IdList = baseCategory2Mapper.selectList(
						new LambdaQueryWrapper<BaseCategory2>().eq(BaseCategory2::getCategory1Id, category1Id))
				.stream().map(BaseCategory2::getId).collect(Collectors.toList());
		//查询条件
		LambdaQueryWrapper<BaseCategory3> wrapper = new LambdaQueryWrapper<>();
		wrapper.in(BaseCategory3::getCategory2Id,category2IdList).eq(BaseCategory3::getIsTop,1).last(" limit 7");
		//	返回三级分类数据集合
		return baseCategory3Mapper.selectList(wrapper);
	}

	@Override
	public JSONObject getBaseCategoryList(Long category1Id) {
		//	创建一个一级分类对象
		JSONObject category1 = new JSONObject();
		//	封装页面需要的数据集
		category1.put("category1Id",category1Id);
		//	分析得到一级id查视图方便
		List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(new LambdaQueryWrapper<BaseCategoryView>().eq(BaseCategoryView ::getCategory1Id,category1Id));
		//	获取一级分类名称
		category1.put("categoryName",baseCategoryViewList.get(0).getCategory1Name());
		//	需要封装二级三级分类数据
		Map<Long, List<BaseCategoryView>> baseCategory2Map = baseCategoryViewList.stream().collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
		Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator = baseCategory2Map.entrySet().iterator();
		//	声明二级分类集合
		List<JSONObject> categoryChild2List = new ArrayList<>();
		while(iterator.hasNext()){
			//获取entry对象
			Map.Entry<Long, List<BaseCategoryView>> entry = iterator.next();
			//获取二级分类id
			JSONObject category2 = new JSONObject();
			category2.put("categoryId",entry.getKey());
			//获取二级分类名称
			category2.put("categoryName",entry.getValue().get(0).getCategory2Name());
			// 	存储三级分类数据
			List<JSONObject> categoryChild3List = entry.getValue().stream().map(baseCategoryView -> {
				//创建三级分类对象
				JSONObject category3 = new JSONObject();
				category3.put("categoryId",baseCategoryView.getCategory3Id());
				category3.put("categoryName",baseCategoryView.getCategory3Name());
				//返回数据
				return category3;
			}).collect(Collectors.toList());
			// 	二级分类中保存三级分类数据
			category2.put("categoryChild",categoryChild3List);
			//  将二级分类对象添加到集合中
			 categoryChild2List.add(category2);
		}
		//	将二级分类数据保存到一级分类中
		category1.put("categoryChild",categoryChild2List);
		//返回一级分类
		return category1;
	}

	@Override
	@TsCache(prefix = "categoryView:")
	public BaseCategoryView getBaseCategoryView(Long category3Id) {
		return baseCategoryViewMapper.selectById(category3Id);
	}

	@Override
	public List<BaseAttribute> findAttribute(Long category1Id) {
		return baseAttributeMapper.selectAttribute(category1Id);
	}

    @Override
    public List<JSONObject> getBaseCategoryList() {
		//创建集合对象
		List<JSONObject> list  = new ArrayList<>();
		//	数据来源于数据库: base_category1, base_category2, base_category3;
		//	使用视图获取数据：
		List<BaseCategoryView> baseCategoryViewList = baseCategoryViewMapper.selectList(null);
		//	Json 数据是通过map map.put(key,value)或 class  setName("zs") 形式！{“name”:"zs"}
		//	使用拉姆达表达式进行分组：
		//	key = category1Id value = List<BaseCategoryView>
		Map<Long,List<BaseCategoryView>> basecategory1Map = baseCategoryViewList.stream()
				.collect(Collectors.groupingBy(BaseCategoryView::getCategory1Id));
		Iterator<Map.Entry<Long, List<BaseCategoryView>>> iterator =
				basecategory1Map.entrySet().iterator();
		while(iterator.hasNext()){
			//	获取entry对象
			Map.Entry<Long,List<BaseCategoryView>> entry = iterator.next();
			//	获取数据
			Long category1Id = entry.getKey();
			List<BaseCategoryView> categoryViewList = entry.getValue();
			//	JSONObject  extends JSON implements Map<String, Object>
			JSONObject category1 = new JSONObject();
			category1.put("categoryName",categoryViewList.get(0).getCategory1Name());
			category1.put("categoryId",category1Id);
			//获取一级分类下的二级分类数据;
			Map<Long,List<BaseCategoryView>> category2Map = categoryViewList.stream()
					.collect(Collectors.groupingBy(BaseCategoryView::getCategory2Id));
			Iterator<Map.Entry<Long,List<BaseCategoryView>>> iterator1 =
					category2Map.entrySet().iterator();
			//	存储到二级分类数据：
			List<JSONObject> categoryChild2List = new ArrayList<>();
			while(iterator1.hasNext()){
				//	获取二级分类对象数据 音乐层级下应该有3次循环;
				Map.Entry<Long,List<BaseCategoryView>> entry1 = iterator1.next();
				Long category2Id = entry1.getKey();
				//	获取到的数据是二级分类Id 对应的集合数据
				List<BaseCategoryView> categoryViewList1 = entry1.getValue();
				JSONObject category2 = new JSONObject();
				category2.put("categoryName",categoryViewList1.get(0).getCategory2Name());
				category2.put("categoryId",category2Id);
				//	获取三级分类数据：直接循环遍历获取三级分类数据即可
				List<JSONObject> categoryChild3List=categoryViewList1.stream().map(baseCategoryView -> {
					//	创建三级分类对象
					JSONObject category3 = new JSONObject();
					category3.put("categoryId",baseCategoryView.getCategory3Id());
					category3.put("categoryName",baseCategoryView.getCategory3Name());
					//	返回数据
					return category3;
				}).collect(Collectors.toList());
				//	获取三级分类数据添加到二级分类中;
				category2.put("categoryChild",categoryChild3List);
				categoryChild2List.add(category2);
			}
			//将二级分类数据添加到一级分类中;
			category1.put("categoryChild",categoryChild2List);
			//	将所有一级分类对象添加到集合中
			list.add(category1);
		}
		return list;
    }


}
